/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.home.presenter;

import com.baiiu.filter.ClassifyBo;
import com.yiqi.liebang.entity.bo.BanlanceBo;
import com.yiqi.liebang.entity.bo.CreateOrderBo;
import com.yiqi.liebang.entity.bo.ExpertBo;
import com.yiqi.liebang.entity.bo.PageBo;
import com.yiqi.liebang.entity.bo.PayBo;
import com.yiqi.liebang.entity.bo.PeopleBo;
import com.yiqi.liebang.feature.home.contract.ClassifyContract;
import com.yiqi.liebang.feature.home.contract.PayContract;
import com.yiqi.liebang.feature.home.dependencies.classsify.DaggerClassifyPresenterComponent;
import com.yiqi.liebang.feature.home.dependencies.pay.DaggerPayPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class PayPresenter implements PayContract.Presenter {

    private PayContract.View mView;

    @Inject

    PayContract.Model mModel;


    public PayPresenter(PayContract.View mView) {
        this.mView = mView;
        DaggerPayPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void createOrder(int orderType, double orderPrice, double payPrice, String userCouponId,
                            int payType, String quizcontent, String Answerid, double questionPri) {
        CreateOrderBo createOrderBo = new CreateOrderBo();
        createOrderBo.setOrderType(orderType);
        createOrderBo.setOrderPrice(orderPrice);
        createOrderBo.setPayPrice(payPrice);
        createOrderBo.setUserCouponId(userCouponId);
        createOrderBo.setPayType(payType);
        createOrderBo.setQuizcontent(quizcontent);
        createOrderBo.setAnswerUid(Answerid);
        createOrderBo.setQuestionPri(questionPri);
        mModel.createOrder(createOrderBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<PayBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(PayBo s) {
                        mView.dismissLoading();
                        mView.showSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e.getMessage());
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });

    }

    @Override
    public void createTopicOrder(int orderType, double orderPrice, double payPrice, String userCouponId, int payType,
                                 String Answerid, double questionPri, String topicAskQuestion, String askIntroduction,String topicId) {
        CreateOrderBo createOrderBo = new CreateOrderBo();
        createOrderBo.setOrderType(orderType);
        createOrderBo.setOrderPrice(orderPrice);
        createOrderBo.setPayPrice(payPrice);
        createOrderBo.setUserCouponId(userCouponId);
        createOrderBo.setPayType(payType);
        createOrderBo.setTopicAskQuestion(topicAskQuestion);
        createOrderBo.setAskIntroduction(askIntroduction);
        createOrderBo.setAnswerUid(Answerid);
        createOrderBo.setQuestionPri(questionPri);
        createOrderBo.setTopicId(topicId);
        mModel.createOrder(createOrderBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<PayBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(PayBo s) {
                        mView.dismissLoading();
                        mView.showSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void createOneLookOrder(int orderType, double orderPrice, double payPrice, String userCouponId, int payType, double questionPri, String questionId) {
        CreateOrderBo createOrderBo = new CreateOrderBo();
        createOrderBo.setOrderType(orderType);
        createOrderBo.setOrderPrice(orderPrice);
        createOrderBo.setPayPrice(payPrice);
        createOrderBo.setUserCouponId(userCouponId);
        createOrderBo.setPayType(payType);
        createOrderBo.setQuestionPri(questionPri);
        createOrderBo.setQuestionId(questionId);
        mModel.createOrder(createOrderBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<PayBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(PayBo s) {
                        mView.dismissLoading();
                        mView.showSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getUserAssetsDetail(int pageIndex,
                                    int pageSize) {
        mModel.getUserAssetsDetail(new PageBo(pageIndex, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<BanlanceBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(BanlanceBo banlanceBos) {
                if (banlanceBos==null) return;
                mView.showBanlanceSuccess(banlanceBos.getBalance()+"");

            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
